<?php 
	include_once("session.php"); 
	include_once("util/message.php");
    include_once("util/constant.php"); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>Đăng nhập | Đăng ký</title>
<?php include("import.php"); ?>
</head>

<body>

<!-- Wrapper -->
<div class="wrapper">
	<!-- Floating banner -->
    <?php include("floating-banner.php");?>
    <!-- End of floating banner -->
    
	<!-- Top banner -->
	<?php include("header.php");?>
    <!-- End of top banner -->
    
    <!-- Navigator -->
	<?php include("navigator.php");?>
    <!-- End of navigator -->
    
    <!-- Menu, newsletter and search -->
    <?php include("menu.php");?>
    <!-- End of menu, newsletter and search -->
    
    <!-- Content -->
    <div class="wrapper-content">
    	<?php
			if(isset($_GET["a"]) && $_GET["a"] == LOGIN_FAILED){
        ?>
		    	<div id="msg"><div class="err"><?php echo $messages["loginError"];?></div></div>
        <?php
			} else if(isset($_GET["a"]) && $_GET["a"] == INSERT_FAILED){
        ?>
        		<div id="msg"><div class="err"><?php echo $messages["registerFailed"];?></div></div>
        <?php
			} else if(isset($_GET["a"]) && $_GET["a"] == USER_BANNED){
        ?>
                <div id="msg"><div class="err"><?php echo $messages["userBanned"];?></div></div>
        <?php
            }        
        ?>
    	<!-- Login and register content wrapper -->
        <div class="login-register-wrapper">
        	<!-- Login wrapper -->
        	<div class="login-wrapper">
            	<div class="login-title">
                	<span>Đăng nhập</span>
                    <span>Bạn đã là thành viên của Miti</span>                    
                </div>
                <form id="loginForm" method="post">
                    <div class="login-content">
                        <div class="control-block">
                            <label class="control-label">Email</label>
                            <input type="text" class="w185" placeholder="Vui lòng nhập email" name="email"/>
                        </div>
                        <div class="control-block">
                            <label class="control-label">Mật khẩu</label>
                            <input type="password" class="w185" placeholder="Vui lòng nhập mật khẩu" name="password"/>
                        </div>
                        <div class="control-block">
                            <label class="control-label"></label>
                            <input type="button" class="login-btn" value="Đăng nhập" onclick="submitForm('loginForm', 'action/authenticateClientAction.php', 'post');"/>
                        </div>
                    </div>
                </form>
            </div>
            <!-- End of login wrapper -->
            
            <div class="login-register-sep"></div>
            
            <!-- Register wrapper -->
            <div class="register-wrapper">
            	<div class="register-title">
                	<span>Là thành viên mới?</span>
                    <span>Đăng ký tạo tài khoản Miti để mua hàng dễ dàng hơn</span>
                </div>
                <form id="registerForm" method="post" action="action/newUserAction.php">
                    <div class="register-content">
                        <div class="control-block">
                            <label class="control-label">Email</label>
                            <input type="text" class="w185" placeholder="Vui lòng nhập email" id="email" name="emailRegister"/>   
                        </div>
                        <div class="control-block">
                            <label class="control-label">Họ tên</label>
                            <input type="text" class="w185" placeholder="Vui lòng nhập họ tên"  name="fullName"/>
                        </div>
                        <div class="control-block">
                            <label class="control-label">Mật khẩu</label>
                            <input type="password" class="w185" placeholder="Vui lòng nhập mật khẩu" id="password" name="passwordRegister"/>
                            
                        </div>
                        <div class="control-block">
                            <label class="control-label">Xác nhận</label>
                            <input type="password" class="w185" placeholder="Vui lòng xác nhận mật khẩu" id="confirmPassword" name="confirmPassword"/>
                        </div>
                        <div class="control-block">
                            <label class="control-label">Giới tính</label>
                            <input type="radio" name="gender" value="1" checked="checked"/>Nam &nbsp;&nbsp;
                            <input type="radio" name="gender" value="0"/>Nữ
                        </div>              
                        <div class="control-block" style="position: relative;">
                            <label class="control-label">Ngày sinh</label>
                            <div class="input-append date datepicker" data-date-format="dd/mm/yyyy" data-date="">
	                            <input class="span2" type="text" readonly="" size="16" id="dob" name="dob">
    	                        <span class="add-on"><i class="icon-calendar"></i></span>
                            </div>
                        </div>
                        <div class="control-block">
                            <label class="control-label">Điện thoại</label>
                            <input type="text" class="w185" placeholder="Vui lòng nhập số điện thoại" name="phoneNumber"/>
                            
                        </div>
                        <div class="control-block">
                            <label class="control-label">Nickname</label>
                            <input type="text" class="w185" placeholder="Vui lòng nhập nickname hiển thị" id="username" name="username"/>
                        </div>
                        <div class="control-block">
                            <label class="control-label w125">Điện chỉ giao hàng</label>
                            <input type="text" class="w435" placeholder="Vui lòng nhập địa chỉ để chúng tôi có thể giao hàng cho bạn" name="address"/>
                        </div>
                        <div class="control-block">
                            <label class="control-label"></label>
                            <input type="checkbox" id="agree" name="agree"/> Tôi đồng ý với với Quy chế phòng giao dịch của Miti
                        </div>
                        <div class="control-block">
                            <label class="control-label"></label>
                            <input type="submit" class="register-btn" value="Đăng ký"/>
                        </div>                     
                    </div>
                </form>
            </div>
            <!-- End of register wrapper -->
        </div>
        <!-- End of login and register content wrapper -->
        <div class="clear"></div>
    </div>
    <!-- End of content -->
    
    <!-- Live support -->
        <?php include("live-support.php");?>
    <!-- End of live support -->
    
	<?php include("footer.php");?>
    <script>
		var isValidEmail = true;
		var isValidUsername = true;
		$(document).ready(function() {
			$.validator.addMethod("validateConfirmPass", function(value) {
				return ($("#password").val() == $("#confirmPassword").val());
			}, "<?php echo $messages["validation.notMatchPassword"]; ?>");
			
			$.validator.addMethod("checkUniqueEmail", function(value) {
				if($("#email").val() == ""){
					$.validator.messages.checkUniqueEmail = "<?php echo $messages["validation.required"];?>";
					return false;
				} else {
					var emailRegex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
					if(emailRegex.test($("#email").val())){
						$.ajax({
							type: "POST",
							url: "action/ajax/checkUniqueEmailProcess.php",
							data: {"email" : $("#email").val()},
							success: function(data){
								if(data == 1) {
									isValidEmail = true;   
								}
								else {
									$.validator.messages.checkUniqueEmail = "<?php echo $messages["validation.existedEmail"];?>";
									isValidEmail = false;   
								}   
							}
						});
					} else{
						$.validator.messages.checkUniqueEmail = "<?php echo $messages["validation.notEmailFormat"];?>";
						isValidEmail = false;
					}
				}
				return isValidEmail;
			}, $.validator.messages.checkUniqueEmail);	
			
			$.validator.addMethod("checkUniqueUsername", function(value) {
				if($("#username").val() == ""){
					$.validator.messages.checkUniqueUsername = "<?php echo $messages["validation.required"];?>";
					return false;
				} else {
					$.ajax({
						type: "POST",
						url: "action/ajax/checkUniqueUsernameProcess.php",
						data: {"username" : $("#username").val()},
						success: function(data){
							if(data == 1) {
								isValidUsername = true;   
							}
							else {
								$.validator.messages.checkUniqueUsername = "<?php echo $messages["validation.existedUsername"];?>";
								isValidUsername = false;   
							}   
						}
					});
				}
				return isValidUsername;
			}, $.validator.messages.checkUniqueUsername);
			
			$.validator.addMethod("validateCheck", function(value) {
				return $("#agree").is(':checked');
			}, "<?php echo $messages["validation.agree"]; ?>");
			
			$(".register-btn").click(function(){
				$("#registerForm").validate({
					rules: {
						emailRegister: { checkUniqueEmail: true },
						fullName: { required: true },
						passwordRegister: { required: true },
						confirmPassword: { validateConfirmPass: true },
						phoneNumber: { required: true },
						username: { checkUniqueUsername: true },
						agree: { validateCheck : true }
					},
					
                    messages: {
						fullName: "<?php echo $messages["validation.required"];?>",
                        passwordRegister: "<?php echo $messages["validation.required"];?>",
						phoneNumber: "<?php echo $messages["validation.required"];?>",
						agree: "<?php echo $messages["validation.agree"];?>"
                    }
				});
			});
		});
    </script>
</div>
<!-- End of wrapper -->

</body>
</html>